package org.millioncall.yueyoga.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.DateTimeUtils;
import org.millioncall.yueyoga.admin.dao.GroupsDAO;
import org.millioncall.yueyoga.admin.dao.MemberDAO;
import org.millioncall.yueyoga.admin.dao.TaskDAO;
import org.millioncall.yueyoga.admin.model.AviatorExpression;
import org.millioncall.yueyoga.admin.model.Group;
import org.millioncall.yueyoga.admin.model.Groups;
import org.millioncall.yueyoga.admin.model.HomePageTaskRecord;
import org.millioncall.yueyoga.admin.model.Member;
import org.millioncall.yueyoga.admin.model.Task;
import org.millioncall.yueyoga.admin.service.TaskService;
import org.springframework.stereotype.Service;

/**
 * @author lisj Mar 29, 2014 8:29:08 AM
 */
@Service
public class TaskServiceImpl implements TaskService {

	private TaskDAO taskDAO;
	private MemberDAO memberDAO;
	private GroupsDAO groupsDAO;

	public List<Task> findTaskLimit() {
		return this.taskDAO.findTaskLimit();
	}

	@Override
	public boolean save(Task task) {
		Map<String, Object> map = new HashMap<String, Object>();
		int len = 0;
		if (task != null) {
			if (StringUtils.isNotBlank(task.getName())) {
				map.put("name", task.getName());
			}
			if (task.getSubStatusId() != null) {
				map.put("subStatusId", task.getSubStatusId());
			}
			if (task.getExpirydate() != null) {
				map.put("expiryDate", task.getExpirydate());
			}
			if (StringUtils.isNotBlank(task.getRemarks())) {
				map.put("remarks", task.getRemarks());
			}
			if (task.getType() != null) {
				map.put("type", task.getType());
			}
			if (StringUtils.isNotBlank(task.getUserId())) {
				map.put("userId", task.getUserId());
			}
			if (task.getResporId() != null) {
				map.put("resporId", task.getResporId());
			}
			if (task.getOrganizationId() != null) {
				map.put("organizationId", task.getOrganizationId());
			}
			map.put("createTime",
					DateTimeUtils.getInstance().format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			map.put("state", "1");
			if (!map.isEmpty()) {
				// 根据任务类别,插入不同的任务数量
				if (task.getType() != null) {
					// 若为会员
					if (task.getType() == 1) {
						if (task.getMemberId() != null) {
							map.put("memberId", task.getMemberId());
						}
						len = taskDAO.save(map);
						// 若为组
					} else if (task.getType() == 2) {
						if (task.getGroupsId() != null) {
							map.put("groupsId", task.getGroupsId());
							Map<String, Object> map2 = new HashMap<String, Object>();
							map2.put("groupsIds", task.getGroupsId());
							List<Integer> groupIds = new ArrayList<Integer>();
							for (Group group : task.getUser().getGroups()) {
								groupIds.add(group.getId());
							}
							map2.put("groups", groupIds);
							List<Member> members = memberDAO.findMemberByGpsId(map2);
							if (!members.isEmpty()) {
								for (Member member : members) {
									map.put("memberId", member.getMemberid());
									len += taskDAO.save(map);
								}
							}
						}
					}
				}
			}
		}
		return len > 0;
	}

	@Override
	public boolean update(Task task) {
		int len = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		if (task != null) {
			if (task.getSubStatusId() != null) {
				map.put("subStatusId", task.getSubStatusId());
			}
			if (task.getState() != null) {
				map.put("state", task.getState());
			}
			if (task.getExpirydate() != null) {
				map.put("expiryDate", task.getExpirydate());
			}
			if (task.getFinishtime() != null) {
				map.put("finishTime", task.getFinishtime());
			}
			if (task.getExecutetime() != null) {
				map.put("executeTime", task.getExecutetime());
			}
			if (StringUtils.isNotBlank(task.getRemarks())) {
				map.put("remarks", task.getRemarks());
			}
			if (task.getResporId() != null) {
				map.put("resporId", task.getResporId());
			}
			if (task.getState() != null) {
				map.put("state", task.getState());
			}
			map.put("taskId", task.getId());
			if (!map.isEmpty()) {
				len = taskDAO.update(map);
			}
		}
		return len > 0;
	}

	@Override
	public boolean deleteById(Integer taskId) {
		if (taskId != null) {
			return taskDAO.deleteById(taskId) == 1;
		}
		return false;
	}

	@Override
	public boolean deleteByIds(String[] ids) {
		if (ids != null && ids.length > 0) {
			return taskDAO.deleteByIds(ids) >= 1;
		}
		return false;
	}

	@Override
	public Task findTaskById(Map<String, Object> map) {
		if (map.get("taskId") != null) {
			Task task = taskDAO.findTaskById(map);
			if (StringUtils.isNotBlank(task.getGroupsId())) {
				Groups group = groupsDAO.findGroupsById(Integer.valueOf(task.getGroupsId()));
				task.setGroup(group);
			}
			return task;
		}
		return null;
	}

	@Override
	public PaginatedList<Task> findTasksInPage(Map<String, Object> map, int start, int limit) {
		return taskDAO.findTasksInPage(map, start, limit);
	}

	@Override
	public Task findTaskRelatedInfoById(Map<String, Object> map) {
		if (map.get("taskId") != null) {
			Task task = taskDAO.findTaskRelatedInfoById(map);
			if (StringUtils.isNotBlank(task.getGroupsId())) {
				Groups group = groupsDAO.findGroupsById(Integer.valueOf(task.getGroupsId()));
				task.setGroup(group);
			}
			return task;
		}
		return null;
	}

	@Override
	public PaginatedList<HomePageTaskRecord> findHomeTasksInPage(Map<String, Object> map,
			int start, int limit) {
		return this.taskDAO.findHomeTasksInPage(map, start, limit);
	}

	@Override
	public int findExpiredTaskCount(Map<String, Object> maps) {
		Map<String, Object> map = new HashMap<String, Object>();
		int m = 0;
		map.put("expiryDate", DateTimeUtils.getInstance().format(new Date(), "yyyy-MM-dd"));
		if (maps.size() > 0 || maps != null) {
			map.put("hallcode", maps.get("hallcode"));
			m = this.taskDAO.findExpiredTaskCount(map);
		}
		return m;
	}

	@Override
	public PaginatedList<HomePageTaskRecord> findTasksByCdts(Map<String, Object> map, int start,
			int limit) {
		return this.taskDAO.findTasksByCdts(map, start, limit);
	}

	public GroupsDAO getGroupsDAO() {
		return groupsDAO;
	}

	public void setGroupsDAO(GroupsDAO groupsDAO) {
		this.groupsDAO = groupsDAO;
	}

	public void setTaskDAO(TaskDAO taskDAO) {
		this.taskDAO = taskDAO;
	}

	public TaskDAO getTaskDAO() {
		return taskDAO;
	}

	public MemberDAO getMemberDAO() {
		return memberDAO;
	}

	public void setMemberDAO(MemberDAO memberDAO) {
		this.memberDAO = memberDAO;
	}

	@Override
	public PaginatedList<HomePageTaskRecord> findTasksByState(Map<String, Object> map, int start,
			int limit) {
		return taskDAO.findTasksByState(map, start, limit);
	}

	@Override
	public List<HomePageTaskRecord> getAllTasksWithAviator(
			Map<String, Object> params, AviatorExpression expression,
			Map<String, Object> aviatorParams, int limit) {
		return this.taskDAO.getAllTasksWithAviator(params, expression, aviatorParams, limit);
	}
}
